package me.zixing.java.chapter11.client;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * Created by bingh on 2017/12/1.
 */
public class Client {

    private static File file = new File("D://mytmp//demo.txt");
    public static void main(String[] args) {
        try{
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress("127.0.0.1",8080));
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("D://demo.txt".getBytes());
            outputStream.flush();
            socket.shutdownOutput();
            InputStream inputStream = socket.getInputStream();


            byte[] b = new byte[1024];
            int i = 0;
            String rest = "";
            while((i=inputStream.read(b))!=-1){
                System.out.println();
                rest += new String(b,0,i, "UTF-8");
            }
            inputStream.close();

            if(rest.equals("没有该文件")){
                System.out.println(rest);
            }else{
                try{
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    fileOutputStream.write(rest.getBytes());
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
